package com.dataseek.iot.sys.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dataseek.iot.sys.entity.NotifyRule1;
import com.dataseek.iot.sys.service.NotifyRuleService;
import com.dataseek.iot.sys.vo.NotifyRuleVo;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolikit.common.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("**/notifyRule")
public class NotifyRuleController {

    @Autowired
    private NotifyRuleService notifyRuleService;


    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ResponseResult query(@RequestParam(value="page")  Integer page,
                                      @RequestParam(value="size")  Integer size,
                                      @RequestParam(value="notifyName", required=false) String notifyName,
                                      @RequestParam(value="notifyCode", required=false)  String notifyCode,
                                      @RequestParam(value="alarmType", required=false)  String alarmType) throws IOException {
        NotifyRuleVo notifyRuleVo = notifyRuleService.queryAction(notifyName, notifyCode, alarmType, page,size);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功",notifyRuleVo);
    }


    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public ResponseResult insert(@RequestBody NotifyRule1 notifyRule1) throws IOException {
        String code  = notifyRuleService.insertAction(notifyRule1);
        ResponseResult responseResult = null;
        if ("999".equals(code)) {
            responseResult=new ResponseResult(999,"规则名称或编码重复，请重新输入!", "999");
        } else {
            responseResult=new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功!", "0");
        }
        return responseResult;
    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseResult update(@RequestBody NotifyRule1 notifyRule1) throws IOException {
        String code  = notifyRuleService.updateAction(notifyRule1);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功!", "1");
    }


    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseResult delete(@RequestBody JSONObject ids)  throws IOException {
        JSONArray jsonArray = ids.getJSONArray("ids");
        List<Integer> listIds = JSONObject.parseArray(jsonArray.toJSONString(), Integer.class);
        Integer code  = notifyRuleService.deleteAction(listIds);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),code.toString());
    }

    @RequestMapping(value = "/sendNotifyMsg", method = RequestMethod.POST)
    public ResponseResult sendNotifyMsg(@RequestParam(value="alarmType") String alarmType,
                                        @RequestBody Map<String, String> paramMp) {
        notifyRuleService.sendNotifyMsg(alarmType, paramMp);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功!", "0");
    }

}
